package com.zz.ecommerce.declareManage.common.service;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.bean.insp.db.declaration.TDeclareFormHeadInsp;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.TDeclareFormHeadInspDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by admin on 2016/4/17.
 */
@Service
public class TDeclareFormHeadInspService {


    @Autowired
    private TDeclareFormHeadInspDao tDeclareFormHeadInspDao;

    public List<TDeclareFormHeadInsp> queryDeclListInsp(GridRequest request, TDeclareFormHeadInsp tDeclareFormHeadInsp, String ieFlag, String tradeMode) {

        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_INSP TOH WHERE TOH.ORDERNO =" +
                "                T_DECLARE_FORM_HEAD_INSP.ORDERNO\n" +
                "        AND T_DECLARE_FORM_HEAD_INSP.ECPCODE = TOH.ECPCODE ";

        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition += " AND TOH.BIZTYPE in (" + tradeMode.replaceAll("#","") + ") ";
        }if(StringUtils.isNotEmpty(ieFlag)){
            whereCondition += " AND TOH.IETYPE='" + ieFlag + "'";
        }
        whereCondition += ")";
        return tDeclareFormHeadInspDao.pageWhere(TableConstants.T_DECLARE_FORM_HEAD_INSP, tDeclareFormHeadInsp, request.getStart(), request.getEnd(), request.getSortOrder(),
                whereCondition);
    }

    public int countDeclInsp(TDeclareFormHeadInsp tDeclareFormHeadInsp, String ieFlag, String tradeMode) {
        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_INSP TOH WHERE TOH.ORDERNO =" +
                "                T_DECLARE_FORM_HEAD_INSP.ORDERNO\n" +
                "        AND T_DECLARE_FORM_HEAD_INSP.ECPCODE = TOH.ECPCODE ";

        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition += " AND TOH.BIZTYPE  in (" + tradeMode.replaceAll("#","") + ")";
        }
        if(StringUtils.isNotEmpty(ieFlag)){
            whereCondition += " AND TOH.IETYPE='" + ieFlag + "'";
        }
        whereCondition += ")";
        return tDeclareFormHeadInspDao.countWhere(TableConstants.T_DECLARE_FORM_HEAD_INSP, tDeclareFormHeadInsp,
                whereCondition);
    }


}
